package com.xwd.service.impl;

import com.xwd.dao.BookDao;
import com.xwd.dao.impl.BookDaoImpl;
import com.xwd.pojo.Book;
import com.xwd.pojo.PageBean;
import com.xwd.service.BookService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BookServiceImpl
 * @Description: com.xwd.service.impl
 * @Auther: xiwd
 * @Date: 2022/2/13 - 02 - 13 - 14:58
 * @version: 1.0
 */
public class BookServiceImpl implements BookService {
    //properties
    private BookDao bookDao=new BookDaoImpl();
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");

    //setter

    //getter

    //constructors


    @Override
    public Long findTotalSize() {
        return bookDao.findTotalSize("SELECT COUNT(*) as 'rows' FROM book");
    }

    //methods
    @Override
    public PageBean<Book> queryForPage(int currentPage, int pageSize) {
        PageBean<Book> pageBean=new PageBean<>();
        List<Book> books=null;
        //数据查询
        Long totalSize = this.findTotalSize();//查询总的记录条数
        Long totalPage = (totalSize%pageSize==0)?(totalSize/pageSize):(totalSize/pageSize+1);//获取总页数
        books = bookDao.queryList("SELECT id,name,author,publish,price,typeid FROM book LIMIT ?,?", (currentPage-1)*pageSize, pageSize);//获取List集合数据
        //数据封装
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalSize(Math.toIntExact(totalSize));
        pageBean.setTotalPage(Math.toIntExact(totalPage));
        pageBean.setList(books);
        //返回封装的对象
        return pageBean;
    }

    @Override
    public Book queryObjectByName( String name) {
        //            SELECT id,name,author,publish,price,typeid FROM `book` WHERE name ='红楼梦'
        String sql = "SELECT id,name,author,publish,price,typeid FROM `book` WHERE name=?";
        return  bookDao.queryForObject(sql,name);
    }

    @Override
    public List<Book> queryForList(String name) {
        String sql = "SELECT id,name,author,publish,price,typeid FROM `book` WHERE name=?";
        return bookDao.queryList(sql,name);
    }

    @Override
    public Map<String, Object> queryForMap(String name) {
        String sql = "SELECT COUNT(*) as count FROM `book` WHERE name=?";
        return bookDao.queryForMap(sql,name);
    }

    @Override
    public List<Map<String, Object>> queryForMapList(BigDecimal price) {
        String sql = "SELECT DISTINCT name FROM book WHERE price>?";
        return bookDao.queryForMapList(sql,price);
    }

    @Override
    public Object[] queryForAttributeArray(BigDecimal price) {
        String sql = "SELECT id,name,author,publish,price,typeid FROM book WHERE price>?";
        return bookDao.queryForAttributeArray(sql,price);
    }

    @Override
    public List<Object[]> queryForAttributeArrayList(BigDecimal price) {
        String sql = "SELECT id,name,author,publish,price,typeid FROM book WHERE price>?";
        return bookDao.queryForAttributeArrayList(sql,price);
    }

    @Override
    public Object queryForAttributeList(String colname,BigDecimal price) {
        String sql = "SELECT id,name,author,publish,price,typeid FROM book WHERE price>?";
        return bookDao.queryForAttributeList(sql,colname,price);
    }

    @Override
    public Object queryForCount(BigDecimal price) {
        String sql = "SELECT COUNT(*) as count FROM book WHERE price>?";
        return bookDao.queryForCount(sql,price);
    }

    @Override
    public int insertBook(Book book) {
        String sql="INSERT INTO book(name,author,publish,price,typeid) VALUES(?,?,?,?,?)";
        return bookDao.updateBook(sql,book.getName(),book.getAuthor(),simpleDateFormat.format(book.getPublish()),book.getPrice(),book.getTypeid());
    }

    @Override
    public int modifyBook(Book book) {
        String sql = " UPDATE book SET publish=? WHERE id=?";
        return bookDao.updateBook(sql,book.getPublish(),book.getId());
    }

    @Override
    public int deleteBook(Book book) {
        String sql="DELETE FROM book WHERE id = ?";
        return bookDao.updateBook(sql,book.getId());
    }
}
